package com.boat.framework.common.monitor.event;

import com.boat.framework.base.api.TrdResponse;
import com.boat.framework.common.util.ServletUtils;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
 * 第三方系统调用事件
 * 需要业务系统自己用拦截器组装并打印日志
 * 
 * @author 周江
 *
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class TrdPartyApiInvoked extends BaseMonitorEvent {
	/**
	 * 结果是否成功。false的情况，包括：连接失败，请求方法后接口返回失败
	 */
	private Boolean success;
	/**
	 * 原始错误编码
	 */
	private String respCode;
	/**
	 * 原始错误描述
	 */
	private String respMsg;
	/**
	 * 耗时,单位毫秒
	 */
	private long times;
	/**
	 * 调用的第三方系统：CCB等
	 */
	private String trdName;
	/**
	 * 备注
	 */
	private String remark;
	
	public TrdPartyApiInvoked() {
		super();
	}

	public TrdPartyApiInvoked(long startTime, String name, String trdName, TrdResponse<?> response) {
		super();
		setName(name);
		
		// 计算耗时
		long betweenMs = System.currentTimeMillis() - startTime;
		setTimes(betweenMs);
		
		setRespCode(response.getRespCode());
		setRespMsg(response.getRespMsg());
		setSuccess(response.isSuccess());
		setTrdName(trdName);
		setRemark("第三方接口调用日志");
	}
	
	public TrdPartyApiInvoked(long startTime, String name,String trdName, Throwable ex) {
		super();
		setType(this.getClass().getSimpleName());
		setName(name);
		
		// 计算耗时
		long betweenMs = System.currentTimeMillis() - startTime;
		setTimes(betweenMs);
		
		setRespCode(
				ex.getCause() != null ? ex.getCause().getClass().getSimpleName() : ex.getClass().getSimpleName());
		setRespMsg(ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());

		setSuccess(false);
		setTrdName(trdName);
		setRemark("第三方接口调用日志");
	}
}
